package com.jack.thinking.java._22.swing.button;

import java.awt.FlowLayout;
import java.lang.reflect.Constructor;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;

import com.utils.SwingConsole;

public class ButtonGroups extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String[] ids = {"June", "Lee", "Jack", "Jane" , "Jay", "Lucy"};
	
	static JPanel makePanel(Class<? extends AbstractButton> kind, String[] ids) {
		ButtonGroup bg = new ButtonGroup();
		JPanel j = new JPanel();
		String title = kind.getName();
		title = title.substring(title.lastIndexOf(".") + 1);
		j.setBorder(new TitledBorder(title));
		for(String id : ids) {
			AbstractButton ab = new JButton("failed");
			try {
				Constructor<? extends AbstractButton> ctor = kind.getConstructor(String.class);
				ab = (AbstractButton)ctor.newInstance(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			bg.add(ab);
			j.add(ab);
		}
		return j;
	}
	
	public ButtonGroups() {
		setLayout(new FlowLayout());
		add(makePanel(JButton.class, ids));
		add(makePanel(JToggleButton.class, ids));
		add(makePanel(JCheckBox.class, ids));
		add(makePanel(JRadioButton.class, ids));
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new ButtonGroups(), 500, 350);
	}
}
